﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Data;
public partial class QuanLyBaiViet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            typeField.Value = Request.QueryString["typeid"];
            if (string.IsNullOrEmpty(typeField.Value))
            {
                Response.Redirect(HelpConstEnum.ADMIN_DEFAULT_PAGE);
                return;
            }
            InitTilte();
            InitListData();
        }
    }
    private void InitTilte()
    {
      
        DataTable dt=LOAI_BAI_VIET.GetTableFields(string.Format(@"{0}={1}",LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID,typeField.Value),
            new string[]{},
            LOAI_BAI_VIET.cl_TEN_LOAI);
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTitle.Text = "QUẢN LÝ BÀI VIẾT " + dt.Rows[0][LOAI_BAI_VIET.cl_TEN_LOAI].ToString().ToUpper();
        }
        else
        {
            Response.Redirect(HelpConstEnum.ADMIN_DEFAULT_PAGE);
        }
    }
    private void InitListData()
    {
        string sql = string.Format(@"SELECT ROW_NUMBER() OVER (ORDER BY ISNULL(bv.DA_DOC,'N') , ISNULL(bv.NOI_BAT,'N') DESC, bv.NGAY_TAO ASC) STT, bv.BAI_VIET_ID,bv.TEN_BAI_VIET,bv.NGAY_TAO,ISNULL(bv.NOI_BAT,'N') NOI_BAT, bv.NGAY_CAP_NHAT,
CASE WHEN u.USERS_ID IS NULL THEN bv.NGUOI_TAO_NAME ELSE u.FULL_NAME END NGUOI_TAO_NAME,bv.NGUOI_TAO_EMAIL,
u1.FULL_NAME NGUOI_CAP_NHAT_NAME,CASE WHEN DUYET IS NULL THEN N'Chưa duyệt' ELSE CASE WHEN DUYET='N' THEN N'Không duyệt' ELSE N'Đã duyệt' END END TRANG_THAI, DUYET,DA_DOC
FROM BAI_VIET bv
LEFT JOIN USERS u ON u.USERS_ID = bv.NGUOI_TAO
LEFT JOIN USERS u1 ON u1.USERS_ID=bv.NGUOI_CAP_NHAT WHERE BV.LOAI_BAI_VIET={0}", typeField.Value);
        DataTable dt = SQLConnectWeb.GetTable(sql, BAI_VIET.sTableName);
        gridViewData.DataKeyNames = new string[] {
            BAI_VIET.cl_BAI_VIET_ID,        
            BAI_VIET.cl_NOI_BAT,
            BAI_VIET.cl_DUYET,
            BAI_VIET.cl_DA_DOC
          };
        gridViewData.AutoGenerateColumns = false;
        gridViewData.DataSource = dt;
        gridViewData.DataBind();

    }
    protected void gridViewData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataKey row = gridViewData.DataKeys[int.Parse(e.CommandArgument.ToString())];
        if (e.CommandName == "cmdEdit")
        {
            //   GridViewRow row = gridViewData.Rows[];


            Response.Redirect(string.Format("BaiViet.aspx?typeid={0}&keyid={1}", typeField.Value, row.Values[BAI_VIET.cl_BAI_VIET_ID].ToString()));

        }
        else if (e.CommandName == "cmdDelete")
        {
            string sqlT = string.Format(@"select {0} from {1} where {2}={3}", BAI_VIET.cl_HINH_THUMBNAIL, BAI_VIET.sTableName, BAI_VIET.cl_BAI_VIET_ID, row.Values[BAI_VIET.cl_BAI_VIET_ID]);

            object url = SQLConnectWeb.ExecuteScalar(sqlT);

            string sql = string.Format("delete from {0} where {1}={2}", BAI_VIET.sTableName, BAI_VIET.cl_BAI_VIET_ID, row.Values[BAI_VIET.cl_BAI_VIET_ID]);
            if (SQLConnectWeb.ExecSQL(sql))
            {

                HelpUtil.DeleteFile(this, HelpConstEnum.ADMIN_UPLOAD_IMAGE_FOLDER + url);
             
                InitListData();


            }
            else
            {
                HelpControls.ShowMsgBox(this, "Dữ liệu đang được sử dụng!");
            }

        }
        else if (e.CommandName == "cmdNoiBat")
        {
            string sql = string.Format(@"UPDATE BAI_VIET 
                    SET NOI_BAT = (CASE WHEN bai_viet_id={0} THEN 'Y' ELSE 'N' END),
                        NGAY_CAP_NHAT=getdate(),
                        NGUOI_CAP_NHAT={1}
                    WHERE LOAI_BAI_VIET={2}", row.Values[BAI_VIET.cl_BAI_VIET_ID], UserLogin.USER_ID(this), typeField.Value);

            if (SQLConnectWeb.ExecSQL(sql))
            {
                //  HelpControls.ShowMsgBox(this, "Xóa dữ liệu thành công!");
                InitListData();


            }
            else
            {
                HelpControls.ShowMsgBox(this, "Làm nổi bật bài viết thất bại!");
            }
        }
        else if(e.CommandName=="cmdDuyet")
        {
            string curent=row.Values[BAI_VIET.cl_DUYET].ToString();
            string sql = string.Format(@"UPDATE BAI_VIET 
                    SET DUYET ='{0}',
                        NGAY_CAP_NHAT=getdate(),
                        NGUOI_CAP_NHAT={1}
                    WHERE bai_viet_id={2}", curent == "Y" ? "N" : "Y", UserLogin.USER_ID(this), row.Values[BAI_VIET.cl_BAI_VIET_ID]);

            if (SQLConnectWeb.ExecSQL(sql))
            {
                //  HelpControls.ShowMsgBox(this, "Xóa dữ liệu thành công!");
                InitListData();


            }
            else
            {
                HelpControls.ShowMsgBox(this, (curent == "Y" ? "Bỏ duyệt" : "Duyệt") + " bật bài viết thất bại!");
            }
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("BaiViet.aspx?typeid=" + typeField.Value);
    }

    protected void gridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)e.Row.Cells[1].Controls[0]).OnClientClick = "return confirm('Bạn có chắc là muốn xóa bài viết này?');";

            Button btnDuyet = (Button)e.Row.Cells[9].Controls[0];
            string duyet = gridViewData.DataKeys[e.Row.RowIndex].Values[BAI_VIET.cl_DUYET].ToString();

            btnDuyet.Text = duyet == "Y" ? "Bỏ duyệt" : "Duyệt đăng";

            Button btn = (Button)e.Row.Cells[10].Controls[0];
            btn.OnClientClick = "return confirm('Bạn có chắc là muốn làm nổi bật bài viết này?');";
            string noibat = gridViewData.DataKeys[e.Row.RowIndex].Values[BAI_VIET.cl_NOI_BAT].ToString();
            btn.Enabled = noibat != "Y";

            string dadoc = gridViewData.DataKeys[e.Row.RowIndex].Values[BAI_VIET.cl_DA_DOC].ToString();
            e.Row.Font.Bold = dadoc != "Y";
            if (dadoc != "Y")
            {
                e.Row.ForeColor = System.Drawing.Color.Blue;
            }

        }
    }
   
}
